// SPDX-FileCopyrightText: The openTCS Authors
// SPDX-License-Identifier: MIT
package org.opentcs.guing.common.components.properties.panel;

import static java.util.Objects.requireNonNull;

import com.google.inject.Inject;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.opentcs.data.model.Couple;
import org.opentcs.guing.base.components.properties.type.BoundingBoxProperty;
import org.opentcs.guing.base.components.properties.type.Property;
import org.opentcs.guing.base.model.BoundingBoxModel;
import org.opentcs.guing.common.components.dialogs.DetailsDialogContent;
import org.opentcs.guing.common.util.I18nPlantOverview;
import org.opentcs.thirdparty.guing.common.jhotdraw.util.ResourceBundleUtil;

/**
 * A panel for editing a {@link BoundingBoxProperty}.
 */
public class BoundingBoxPropertyEditorPanel
    extends
      JPanel
    implements
      DetailsDialogContent {

  /**
   * The bundle to be used.
   */
  private static final ResourceBundleUtil BUNDLE
      = ResourceBundleUtil.getBundle(I18nPlantOverview.PROPERTIES_PATH);
  /**
   * The property to be edited.
   */
  private BoundingBoxProperty boundingBoxProperty;

  /**
   * Creates a new instance.
   */
  @Inject
  @SuppressWarnings("this-escape")
  public BoundingBoxPropertyEditorPanel() {
    initComponents();
  }

  @Override
  public void updateValues() {
    boundingBoxProperty.setValue(createBoundingBoxFromInput());
  }

  @Override
  public String getTitle() {
    return BUNDLE.getString("boundingBoxPropertyEditorPanel.title");
  }

  @Override
  public void setProperty(Property property) {
    requireNonNull(property, "property");
    this.boundingBoxProperty = (BoundingBoxProperty) property;

    lengthTextField.setText(String.valueOf(boundingBoxProperty.getValue().getLength()));
    widthTextField.setText(String.valueOf(boundingBoxProperty.getValue().getWidth()));
    heightTextField.setText(String.valueOf(boundingBoxProperty.getValue().getHeight()));
    referenceOffsetXTextField.setText(
        String.valueOf(boundingBoxProperty.getValue().getReferenceOffset().getX())
    );
    referenceOffsetYTextField.setText(
        String.valueOf(boundingBoxProperty.getValue().getReferenceOffset().getY())
    );
  }

  @Override
  public Property getProperty() {
    return boundingBoxProperty;
  }

  private BoundingBoxModel createBoundingBoxFromInput() {
    try {
      return new BoundingBoxModel(
          Long.parseLong(lengthTextField.getText()),
          Long.parseLong(widthTextField.getText()),
          Long.parseLong(heightTextField.getText()),
          new Couple(
              Long.parseLong(referenceOffsetXTextField.getText()),
              Long.parseLong(referenceOffsetYTextField.getText())
          )
      );
    }
    catch (NumberFormatException e) {
      JOptionPane.showMessageDialog(
          this,
          BUNDLE.getString("boundingBoxPropertyEditorPanel.optionPane_numberFormatError.title"),
          BUNDLE.getString("boundingBoxPropertyEditorPanel.optionPane_numberFormatError.message"),
          JOptionPane.ERROR_MESSAGE
      );
      // Re-throw the exception to prevent the editor panel from closing.
      throw e;
    }
  }

  // FORMATTER:OFF
  // CHECKSTYLE:OFF
  /**
   * This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    dimensionsPanel = new javax.swing.JPanel();
    lengthLabel = new javax.swing.JLabel();
    widthLabel = new javax.swing.JLabel();
    heightLabel = new javax.swing.JLabel();
    lengthTextField = new javax.swing.JTextField();
    widthTextField = new javax.swing.JTextField();
    heightTextField = new javax.swing.JTextField();
    referenceOffsetPanel = new javax.swing.JPanel();
    referenceOffsetXLabel = new javax.swing.JLabel();
    referenceOffsetYLabel = new javax.swing.JLabel();
    referenceOffsetXTextField = new javax.swing.JTextField();
    referenceOffsetYTextField = new javax.swing.JTextField();

    setLayout(new java.awt.GridBagLayout());

    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("i18n/org/opentcs/plantoverview/panels/propertyEditing"); // NOI18N
    dimensionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("boundingBoxPropertyEditorPanel.panel_dimensions.border.title"))); // NOI18N
    dimensionsPanel.setLayout(new java.awt.GridBagLayout());

    lengthLabel.setText(bundle.getString("boundingBoxPropertyEditorPanel.label_length")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
    dimensionsPanel.add(lengthLabel, gridBagConstraints);

    widthLabel.setText(bundle.getString("boundingBoxPropertyEditorPanel.label_width")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
    dimensionsPanel.add(widthLabel, gridBagConstraints);

    heightLabel.setText(bundle.getString("boundingBoxPropertyEditorPanel.label_height")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
    dimensionsPanel.add(heightLabel, gridBagConstraints);

    lengthTextField.setColumns(12);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(3, 3, 0, 3);
    dimensionsPanel.add(lengthTextField, gridBagConstraints);

    widthTextField.setColumns(12);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(3, 3, 0, 3);
    dimensionsPanel.add(widthTextField, gridBagConstraints);

    heightTextField.setColumns(12);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
    dimensionsPanel.add(heightTextField, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    add(dimensionsPanel, gridBagConstraints);

    referenceOffsetPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("boundingBoxPropertyEditorPanel.panel_referenceOffset.border.title"))); // NOI18N
    referenceOffsetPanel.setLayout(new java.awt.GridBagLayout());

    referenceOffsetXLabel.setText(bundle.getString("boundingBoxPropertyEditorPanel.label_referenceOffsetX")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
    referenceOffsetPanel.add(referenceOffsetXLabel, gridBagConstraints);

    referenceOffsetYLabel.setText(bundle.getString("boundingBoxPropertyEditorPanel.label_referenceOffsetY")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
    referenceOffsetPanel.add(referenceOffsetYLabel, gridBagConstraints);

    referenceOffsetXTextField.setColumns(12);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(3, 3, 0, 3);
    referenceOffsetPanel.add(referenceOffsetXTextField, gridBagConstraints);

    referenceOffsetYTextField.setColumns(12);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
    referenceOffsetPanel.add(referenceOffsetYTextField, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(3, 0, 0, 0);
    add(referenceOffsetPanel, gridBagConstraints);
  }// </editor-fold>//GEN-END:initComponents
  // CHECKSTYLE:ON
  // FORMATTER:ON

  // FORMATTER:OFF
  // CHECKSTYLE:OFF
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPanel dimensionsPanel;
  private javax.swing.JLabel heightLabel;
  private javax.swing.JTextField heightTextField;
  private javax.swing.JLabel lengthLabel;
  private javax.swing.JTextField lengthTextField;
  private javax.swing.JPanel referenceOffsetPanel;
  private javax.swing.JLabel referenceOffsetXLabel;
  private javax.swing.JTextField referenceOffsetXTextField;
  private javax.swing.JLabel referenceOffsetYLabel;
  private javax.swing.JTextField referenceOffsetYTextField;
  private javax.swing.JLabel widthLabel;
  private javax.swing.JTextField widthTextField;
  // End of variables declaration//GEN-END:variables
  // CHECKSTYLE:ON
  // FORMATTER:ON
}
